package kr.saeildamil.androidStudy_second.valueani;

import kr.saeildamil.androidStudy_second.R;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class AnimView extends View {
	Ball mBall;
	Paint mPaint;
	ObjectAnimator mToRight;
	ObjectAnimator mToDown;
	ObjectAnimator mToLarge;

	public AnimView(Context context) {
		super(context);
		init();
	}

	public AnimView(Context context, boolean status) {
		super(context);
		init();
		mToRight = ObjectAnimator.ofInt(mBall, "x", 100, 400);
		mToRight.setDuration(2000);
		mToRight.addUpdateListener(mInvalator);

		mToDown = ObjectAnimator.ofInt(mBall, "y", 50, 300);
		mToDown.setDuration(2000);
		mToDown.addUpdateListener(mInvalator);

		mToLarge = ObjectAnimator.ofInt(mBall, "rad", 20, 40);
		mToLarge.setDuration(2000);
		mToLarge.addUpdateListener(mInvalator);
	}

	AnimatorUpdateListener mInvalator = new AnimatorUpdateListener() {
		public void onAnimationUpdate(ValueAnimator animation) {
			invalidate();
		}
	};

	private void init() {
		mBall = new Ball();
		mBall.setX(100);
		mBall.setY(200);
		mBall.setRadius(20);

		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(Color.BLUE);
	}

	public void startAnim(int id) {
		mBall.setX(100);
		mBall.setY(50);
		mBall.setRadius(20);

		AnimatorSet set = new AnimatorSet();
		switch (id) {
		case R.id.btn1:
			set.playSequentially(mToRight, mToDown, mToLarge);
			break;
		case R.id.btn2:
			set.playSequentially(mToDown, mToRight, mToLarge);
			break;
		case R.id.btn3:
			set.playTogether(mToRight, mToDown, mToLarge);
			break;
		case R.id.btn4:
			set.play(mToRight).with(mToLarge).before(mToDown);
			break;
		}
		set.start();
	}

	public void startAnim() {
		ObjectAnimator anim = ObjectAnimator.ofInt(mBall, "x", 100, 460);
		anim.setDuration(2000);
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			public void onAnimationUpdate(ValueAnimator animation) {
				invalidate();
			}
		});
		anim.start();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawCircle(mBall.getX(), mBall.getY(), mBall.getRadius(), mPaint);
	}

}
